<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>单例模式</title>
</head>
<body>
    <script>

        // 单例模式:只能实例化一次，如果实例化了一个对象再实例化，返回之前那个对象；
        // 声明构造函数
        function Person(name) {
            this.name = name;
            this.instance = null;

        }
        // 通过原型链给对象添加方法为实例方法
        Person.prototype.say = function() {
            console.log(this.name,"say something");
        }

        // 给Person添加方法，即为静态方法
        Person.getInstance = function(name) {
            console.log('返回单例对象');
            
            if(!this.instance){
                console.log(this.instance);
                this.instance = new Person(name);
                this.instance.instance = 888;
            }
            return this.instance;
            
        }


        // let liuyu = new Person('刘钰');
        // console.log(liuyu);
        // console.log(liuyu.__proto__.constructor === Person); // 通过 __proto__获取对应类的属性和方法
        // liuyu.say();

        const p = Person.getInstance('liuyu');
        const p1 = Person.getInstance('liudada')

        console.log(p);
        console.log(p1);
        






    </script>
</body>
</html>